package br.com.spam.pipeline.instruction;

import br.com.spam.pipeline.core.Configuration;
import br.com.spam.pipeline.core.Register;

public class RsLoader
{
	private final Register rs;
	protected final Instruction instruction;
	private Integer loadedValue;

	public RsLoader(Register rs, Instruction instruction)
	{
		this.rs = rs;
		this.instruction = instruction;
	}

	public boolean instructionDecodeHook()
	{
		switch (this.rs.getCorrectValueDataSource())
		{
			case JustCalculated :
				if (Configuration.bypassEnabled)
				{
					this.instruction.setALUSrcA(ALUSrc.EX);
					return true;
				}
				else
					return false;
			case JustLoadedFromMemory :
				if (Configuration.bypassEnabled)
				{
					this.instruction.setALUSrcA(ALUSrc.DM_Memory);
					return true;
				}
				else
					return false;
			case EX :
				if (Configuration.bypassEnabled)
				{
					this.instruction.setALUSrcA(ALUSrc.DM_EX);
					return true;
				}
				else
					return false;
			case DM_EX :
			case DM_Memoria :
			case Register :
				this.instruction.setALUSrcA(ALUSrc.Register);
				this.loadedValue = this.rs.getCorrectValue();
				return true;
			default :
				return false;
		}
	}

	public void executionHook()
	{
		if (this.loadedValue == null)
		{
			this.loadedValue = this.rs.getCorrectValue();
		}
	}

	public int rsValue()
	{
		return this.loadedValue;
	}
}
